package com.gxa.ssm340.controller;

import com.gxa.ssm340.common.R;
import com.gxa.ssm340.entity.dto.*;
import com.gxa.ssm340.entity.vo.CpnUsedListVO;
import com.gxa.ssm340.entity.vo.CpnUsedListsVO;
import com.gxa.ssm340.entity.vo.UserCouponVO;
import com.gxa.ssm340.exception.BizException;
import com.gxa.ssm340.mapper.UserCancelMapper;
import com.gxa.ssm340.service.UserCancelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Lenovo
 */
@Api(value = "CpnCancelController",tags = "用户核销查询接口")
@RestController
@RequestMapping("/user")
public class UserCancelController {
    @Autowired
    UserCancelService userCancelService;
    @ApiOperation("根据手机号模糊查询")
    @PostMapping("/getCpnList")
    public R getCpnList(PhonePageDTO phonePageDTO) throws BizException {
        String phone = phonePageDTO.getPhone();
        Integer page = phonePageDTO.getPage();

        System.out.println(page+"================================");
        CouponPageDTO couponPageDTO = new CouponPageDTO();
        CouponPageDTO couponPageDTO1 = new CouponPageDTO();
        couponPageDTO1.setPage(1);
        couponPageDTO1.setLimit(100);
        couponPageDTO.setPhone(phone);
        int start = (page - 1) * 10; // 假设每页显示10条数据
        int end = 10; // 当前页的数据结束位置
        couponPageDTO.setPage(start);
        couponPageDTO.setLimit(end);
        UserCouponVO[] userCouponVOS1 = userCancelService.selectAllLikePhone(couponPageDTO1);
        UserCouponVO[] userCouponVOS = userCancelService.selectAllLikePhone(couponPageDTO);
        int count = userCouponVOS1.length; // 数据总数
        PageCountDTO pageCountDTO = new PageCountDTO();
        pageCountDTO.setCount(count);
        pageCountDTO.setUserCouponVO(userCouponVOS);
        return R.success(pageCountDTO);
    }


/**
 * @Description: 核销清单
 * @param cpId
 * @return: com.gxa.ssm340.common.R
 * @Author: 易某
 * @Date: 2023/10/25 10:16
 */
    @PostMapping("/useCpn")
    public R getUseCpn(Integer cpId) throws BizException {
        // userCancelService.modifyById(cpId,couponPageDTO);
        userCancelService.updateById(cpId);
        return R.success();
    }
    /**
     * @Description: 根据起始时间终止时间和分页查询
     * @param cpnUsedDTO
     * @return: com.gxa.ssm340.common.R
     * @Author: 易某
     * @Date: 2023/10/25 10:12
     */
    @PostMapping("/getCpnUsedList")
    public R getCpnUsedList(CpnUsedDTO cpnUsedDTO) throws BizException, ParseException {
        Integer page = cpnUsedDTO.getPage();
        String from = cpnUsedDTO.getFrom();
        String to = cpnUsedDTO.getTo();
        CpnUsedPageDTO cpnUsedPageDTO = new CpnUsedPageDTO();
        if(from !=null && !"".equals(from) && to !=null && !"".equals(to)){
            SimpleDateFormat simpleDateFormat =  new SimpleDateFormat("yyyy-MM-dd");
            Date parse = simpleDateFormat.parse(from);
            Date parse1 = simpleDateFormat.parse(to);
            cpnUsedPageDTO.setFrom(parse);
            cpnUsedPageDTO.setTo(parse1);
        }
        int start = (page - 1) * 10; // 假设每页显示10条数据
        int end = start + 10; // 当前页的数据结束位置
        cpnUsedPageDTO.setPage(start);
        cpnUsedPageDTO.setLimit(end);
        List<CpnUsedListVO> cpnUsedListVOS = userCancelService.selectByTime(cpnUsedPageDTO);
        CpnUsedListsVO cpnUsedsList = new CpnUsedListsVO();
        cpnUsedsList.setCount(cpnUsedListVOS.size());
        cpnUsedsList.setCpnUsedListVO(cpnUsedListVOS);
        return R.success(cpnUsedsList);
    }
}










